<?php

class Comment extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function add()
    {
        if($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $userinfo['email'] = trim($this->input->post('email'));
            $userinfo['name'] = trim($this->input->post('name'));

            $uid = 0;
            $comment_error = array();
            $remember_me = false;
            if(!$this->user_model->is_login())
            {
                $this->load->helper('email');
                if(empty($userinfo['email']) || !valid_email($userinfo['email']))
                    $comment_error['email'] = '请输入合法邮箱地址';

                if(empty($userinfo['name']))
                    $comment_error['name'] = '请输入对您的称呼';
                if(!count($comment_error))
                {
                    //添加用户
                    $oUser = $this->user_model->find_by_email($userinfo['email']);
                    $userinfo['passwd'] = '111111';
                    if(!$oUser)
                    {
                        $uid = $this->user_model->add($userinfo);
                    }
                    else
                    {
                        $uid = $oUser->user_id;
                    }

                    $remember_me = $this->input->post('remember_me');
                    if($remember_me)
                    {
                        $remember_me = true;
                    }
                    if( !$this->user_model->login($userinfo['email'], $userinfo['passwd'], $remember_me) )
                    {
                        $comment_error['account'] = '请登陆帐号';
                    }
                }
            }

            $post['pid'] = $this->input->post('pid');
            $post['content'] = $this->input->post('content');
            $post['author'] = $this->user_model->get_id() ?  $this->user_model->get_id() : $uid;
            
            if(strlen($post['content']) < 5)
            {
                $comment_error['content'] = '内容不能少于5字符';
            }
            if(!count($comment_error))
            {
                $id = $this->post_model->add($post, 'comment');
                if( $id )
                {
                    $cmt = $this->post_model->get($id);
                    $cmt['content'] = content($cmt['content']);

                    echo json_encode(array(
                        'success' => 1,
                        'message' => "<div>{$cmt['content']}{$cmt['create_time']}<img src='{$cmt['user_avatar']}' />{$cmt['user_name']}</div>"
                    ));
                    return;
                }
            }
            echo json_encode(array(
                'success' => 0,
                'message' => implode("\n\r", $comment_error)
            ));
        }
    }

    public function get()
    {

    }

    public function lists()
    {

    }

    public function del()
    {

    }

    public function update()
    {
        
    }
}
